
package com.welsy.authoring.command
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.welsy.authoring.business.*;
	import com.welsy.util.Comparator;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.events.FaultEvent;
	import mx.controls.Alert;
	import mx.collections.ICollectionView;
	import mx.collections.Sort;
	import mx.collections.SortField;
	import mx.utils.ArrayUtil;
	import com.welsy.authoring.vo.CourseComponentsVO;
	import com.welsy.authoring.event.GetFinalResultEvent;
	import mx.rpc.IResponder;
	import com.welsy.util.NamespaceLocator;
	import com.welsy.authoring.model.CourseModelLocator;
	import com.welsy.authoring.vo.course.TestVO;
	import com.welsy.authoring.vo.TestResultVO;
	import com.welsy.authoring.vo.QuestionVO;
	import com.welsy.authoring.vo.AnswerVO;
	import mx.collections.ArrayCollection;
	import com.welsy.authoring.vo.QuestionResultVO;
	import com.welsy.authoring.view.testcomponents.QuestionAnswer;
	
	/**
	 * @version	$Revision: 1.1 $
	 */
	public class GetFinalResultCommand implements ICommand, IResponder
	{
	
		public function execute( event : CairngormEvent ): void
		{
			var gfr_eve : GetFinalResultEvent = GetFinalResultEvent( event );
			
			var delegate : GetFinalResultDelegate = new GetFinalResultDelegate( this );
			delegate.getFinalResult(gfr_eve.userId, gfr_eve.testId);
		}
	
		public function result( event : Object ) : void
		{
			var cmodel : CourseModelLocator = CourseModelLocator.getInstance();
			var testResultVO : TestResultVO = new TestResultVO;
			
			var xres : XMLList = event.result as XMLList;
			trace("xxx"+xres.toXMLString());
			
			var tNS : Namespace = new Namespace(NamespaceLocator.TEST_DATATYPES);
			var utrNS : Namespace = new Namespace(NamespaceLocator.USERTESTRESULTS_DATATYPES);
			var urNS : Namespace = new Namespace(NamespaceLocator.USER_RES_DATATYPES);
			
			var TAG_GETFINALRESULTRESULT : QName = new QName(utrNS, "GetFinalResultResult");
			var TAG_TESTRESULT : QName = new QName(utrNS, "TestResult");
			var TAG_TEST : QName = new QName(urNS, "Test");
			var TAG_PASSED : QName = new QName(urNS, "Passed");
			var TAG_T_SCORE : QName = new QName(urNS, "Score");
			var TAG_CONSULTATIONTIME : QName = new QName(tNS, "ConsultationTime");
			
			var testResultE : XMLList = xres.descendants(TAG_TEST);
			var passedE : String = xres.descendants(TAG_TESTRESULT).child(TAG_PASSED);
			var scoreE : String = xres.descendants(TAG_TESTRESULT).child(TAG_T_SCORE);
			var consultationTimeE : String = xres.descendants(TAG_TESTRESULT).child(TAG_CONSULTATIONTIME);
			
			var TAG_QUESTIONRESULTCOLLECTION : QName = new QName(utrNS, "QuestionResultCollection");
			var TAG_QUESTION : QName = new QName(utrNS, "Question");
			var TAG_ANSWER : QName = new QName(utrNS, "Answer");
			var TAG_UR_SCORE : QName = new QName(utrNS, "Score");
			var TAG_PERCENTAGESCORE : QName = new QName(utrNS, "PercentageScore");
			
			var TAG_TESTID : QName = new QName(tNS, "TestID");
			var TAG_TITLE : QName = new QName(tNS, "Title");
			var TAG_QUESTIONSKIPTYPE : QName = new QName(tNS, "QuestionSkipType");
			var TAG_QUESTIONWEIGHTTYPE : QName = new QName(tNS, "QuestionWeightType");
			var TAG_PASSINGCONDITON : QName = new QName(tNS, "PassingCondition");
			var TAG_DISPLAYCORRECTED : QName = new QName(tNS, "DisplayCorrected");
			var TAG_DISPLAYFEEDBACK : QName = new QName(tNS, "DisplayFeedback");
			var TAG_DISPLAYCORRECTANSWER : QName = new QName(tNS, "DisplayCorrectAnswer");
			var TAG_TRACECONSULTATIONTIME : QName = new QName(tNS, "TraceConsultationTime");
			var TAG_CONSULTATIONTIMELIMIT : QName = new QName(tNS, "ConsultationTimeLimit");
			
			var TAG_QUESTIONID : QName = new QName(tNS, "QuestionID");
			//var TAG_TITLE : QName = new QName(tNS, "Title");
			var TAG_POSITION : QName = new QName(tNS, "Position");
			var TAG_WEIGHT : QName = new QName(tNS, "Weight");
			var TAG_FILEREPOSITORYID : QName = new QName(tNS, "FileRepositoryID");
			var TAG_TRACKSTATUS : QName = new QName(tNS, "TrackStatus");
			var TAG_ANSWERID : QName = new QName(tNS, "AnswerID");
			
			//Parse test
			var testVO : TestVO = new TestVO;
			testVO.testId = testResultE.child(TAG_TESTID);
			testVO.title = testResultE.child(TAG_TITLE);
			testVO.skipType = testResultE.child(TAG_QUESTIONSKIPTYPE);
			testVO.questionWeightType = testResultE.child(TAG_QUESTIONWEIGHTTYPE);
			testVO.passingCondition = testResultE.child(TAG_PASSINGCONDITON);
			testVO.displayCorrected = testResultE.child(TAG_DISPLAYCORRECTED);
			testVO.displayFeedback = testResultE.child(TAG_DISPLAYFEEDBACK);
			testVO.displayCorrectAnswere = testResultE.child(TAG_DISPLAYCORRECTANSWER);
			testVO.traceConsultationTime = testResultE.child(TAG_TRACECONSULTATIONTIME);
			testVO.consultationTimeLimit = testResultE.child(TAG_CONSULTATIONTIMELIMIT);
			
			testResultVO.testVO = testVO;
			testResultVO.passed = passedE == "true" ? true : false;
			testResultVO.score = scoreE;
			testResultVO.consultationTime = consultationTimeE;
			
			var questres_arr : ArrayCollection = new ArrayCollection;
			var questions : int = 0;

			for each(var qrt_t : XML in xres.descendants(TAG_QUESTIONRESULTCOLLECTION).children()) {
				//QuestionResultType
				var questionResultVO : QuestionResultVO = new QuestionResultVO;
				
				var quest_t : XMLList = qrt_t.child(TAG_QUESTION);
				var questionVO : QuestionVO = new QuestionVO;
				questionVO.questionId = quest_t.child(TAG_QUESTIONID);
				questionVO.title = quest_t.child(TAG_TITLE);
				questionVO.position = quest_t.child(TAG_POSITION);
				questionVO.weight = quest_t.child(TAG_WEIGHT);
				questionVO.fileRepositoryId = quest_t.child(TAG_FILEREPOSITORYID);
				questionVO.trackStatus = quest_t.child(TAG_TRACKSTATUS);
				
				var answ_t : XMLList = qrt_t.child(TAG_ANSWER);
				var answerVO : AnswerVO = new AnswerVO;
				answerVO.answerId = answ_t.child(TAG_ANSWER);
				answerVO.title = answ_t.child(TAG_TITLE);
				answerVO.position = answ_t.child(TAG_POSITION);
				answerVO.weight = answ_t.child(TAG_WEIGHT);
				answerVO.fileRepositoryId = answ_t.child(TAG_FILEREPOSITORYID);
				
				questionResultVO.question = questionVO;
				questionResultVO.answer = answerVO;
				questionResultVO.score = qrt_t.child(TAG_PERCENTAGESCORE);
				
				//COnstruct the graphical representation
				var view : QuestionAnswer = new QuestionAnswer;
				view.questionResult = questionResultVO;
				questres_arr.addItem(view);
				
				questions++;
			}

			cmodel.testResultVO = testResultVO;
			cmodel.questionAnswers = questres_arr;
		}
	
		public function fault( event : Object ) : void
		{
			Alert.show( "GetFinalResultCommand.fault!" );
		}
	}

}